<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="" disabled="disabled"></el-input>
    </el-form-item>
    <el-form-item label="性别" size="mini" prop="sex">
      <el-radio-group v-model="dataForm.sex" disabled="disabled">
        <el-radio :label="1">男</el-radio>
        <el-radio :label="2">女</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="所在省份" prop="province">
      <el-input v-model="dataForm.province" placeholder="" disabled="disabled"></el-input>
    </el-form-item>
    <el-form-item label="所在城市" prop="city">
      <el-input v-model="dataForm.city" placeholder="" disabled="disabled"></el-input>
    </el-form-item>
    <el-form-item label="关注时间" prop="subscribeTime">
      <el-input v-model="dataForm.subscribeTime" placeholder="" disabled="disabled"></el-input>
    </el-form-item>
    <el-form-item label="是否员工" size="mini" prop="isstaff">
      <el-radio-group v-model="dataForm.isstaff">
        <el-radio :label="0">否</el-radio>
        <el-radio :label="1">是</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="subable" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        subable: false,
        visible: false,
        dataForm: {
          id: 0,
          openid: '',
          subscribe: '',
          nickname: '',
          sex: '',
          language: '',
          city: '',
          province: '',
          country: '',
          headimgurl: '',
          subscribeTime: '',
          unionid: '',
          remark: '',
          groupid: '',
          qrurl: '',
          imagepath: '',
          refreshtime: '',
          isstaff: 0
        },
        dataRule: {
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.subable = false
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/wxuserinfo/wxuserinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.openid = data.wxuserinfo.openid
                this.dataForm.subscribe = data.wxuserinfo.subscribe
                this.dataForm.nickname = data.wxuserinfo.nickname
                this.dataForm.sex = data.wxuserinfo.sex
                this.dataForm.language = data.wxuserinfo.language
                this.dataForm.city = data.wxuserinfo.city
                this.dataForm.province = data.wxuserinfo.province
                this.dataForm.country = data.wxuserinfo.country
                this.dataForm.headimgurl = data.wxuserinfo.headimgurl
                this.dataForm.subscribeTime = data.wxuserinfo.subscribeTime
                this.dataForm.unionid = data.wxuserinfo.unionid
                this.dataForm.remark = data.wxuserinfo.remark
                this.dataForm.groupid = data.wxuserinfo.groupid
                this.dataForm.qrurl = data.wxuserinfo.qrurl
                this.dataForm.imagepath = data.wxuserinfo.imagepath
                this.dataForm.refreshtime = data.wxuserinfo.refreshtime
                this.dataForm.isstaff = data.wxuserinfo.isstaff
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.subable = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/wxuserinfo/wxuserinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'openid': this.dataForm.openid,
                'subscribe': this.dataForm.subscribe,
                'nickname': this.dataForm.nickname,
                'sex': this.dataForm.sex,
                'language': this.dataForm.language,
                'city': this.dataForm.city,
                'province': this.dataForm.province,
                'country': this.dataForm.country,
                'headimgurl': this.dataForm.headimgurl,
                'subscribeTime': this.dataForm.subscribeTime,
                'unionid': this.dataForm.unionid,
                'remark': this.dataForm.remark,
                'groupid': this.dataForm.groupid,
                'qrurl': this.dataForm.qrurl,
                'imagepath': this.dataForm.imagepath,
                'refreshtime': this.dataForm.refreshtime,
                'isstaff': this.dataForm.isstaff
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            this.subable = false
          }
        })
      }
    }
  }
</script>
